//
//  YZESignupTableViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/3/31.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "YZESignupTableViewController.h"
#import <QMUIKit/QMUIKit.h>
#import "CommonDefine.h"
#import "YZESignupProfileTableViewController.h"
#import "YZEModel.h"
#import "AppDelegate.h"
#import "SelfTools.h"
#import "MD5.h"

#define URL_Register @"/api/users/sickperson/register"

@interface YZESignupTableViewController ()<NetworkUtilsDelegate>
@property (nonatomic, strong) QMUIFillButton *m_btn_next;
@end

@implementation YZESignupTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"注册";
    //获取验证码按钮点击
    [self.m_btn_getcatpha addTarget:self action:@selector(onGetvcClick:) forControlEvents:UIControlEventTouchUpInside];
    
}




//获取验证码点击响应
-(void)onGetvcClick:(UIButton *) sender{
    if ([SelfTools checkMobile:self.m_tf_mobi.text]) {
        YZEModel *request=[YZEModel sharedInstace];
        [request requestGetCaptcha:self.m_tf_mobi.text delegate:nil requestTag:0];
        [self receiveCheckNumButton];
    }
    else{
        [UIView showToastWindowWithMessage:@"请输入正确的手机格式"];
    }
   
    
   
    
    
   // ((AppDelegate*)([UIApplication sharedApplication].delegate)).timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
    
}

//获取验证码倒计时
- (void)receiveCheckNumButton{
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.m_btn_getcatpha setTitle:@"获取验证码" forState:UIControlStateNormal];
                self.m_btn_getcatpha.userInteractionEnabled = YES;
            
            });
        }else{
            int seconds = timeout;
            NSString *strTime = [NSString stringWithFormat:@"%d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
            self.m_btn_getcatpha.userInteractionEnabled = NO;
                //设置界面的按钮显示 根据自己需求设置
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self.m_btn_getcatpha setTitle:[NSString stringWithFormat:@"重发(%@s)",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)onGetcatphaClick:(id)sender {
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section!=0) {
        return 0;
    }
    return FOOTER_HEIGHT;
}
//footer里构造下一步按钮
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *rowview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FOOTER_HEIGHT)];
    _m_btn_next = [[QMUIFillButton alloc] initWithFillType:QMUIFillButtonColorBlue];
    [self.m_btn_next setTitle:@"下一步" forState:UIControlStateNormal];
    [self.m_btn_next setFrame:CGRectMake((SCREEN_WIDTH-BTN_ROW_WIDTH)/2, (FOOTER_HEIGHT-BTN_ROW_HEIGHT)/2, BTN_ROW_WIDTH, BTN_ROW_HEIGHT)];
    [self.m_btn_next addTarget:self action:@selector(onNextClick:) forControlEvents:UIControlEventTouchUpInside];
    [rowview addSubview:self.m_btn_next];
    if (section!=0) {
        return nil;
    }
    return rowview;
}

//下一步点击响应
- (IBAction)onNextClick:(id)sender{
    if ([self.m_tf_pwd.text isNotEmpty]) {
        if ([self.m_tf_catpha isNotEmpty]) {
            UIStoryboard *signinupSB = [UIStoryboard storyboardWithName:@"signinup" bundle:nil];
            YZESignupProfileTableViewController *signup_profile_view = [signinupSB instantiateViewControllerWithIdentifier:@"signup_profileVC"];
            [[YZENetwork sharedInstace]postRequestRegisterWithUrl:[BASE stringByAppendingString:URL_Register] parameters:@{@"verifyCode":self.m_tf_catpha.text,@"phone":self.m_tf_mobi.text,@"password":[MD5 getMd5_32Bit_String:self.m_tf_pwd.text]} delegate:self requestTag:1 successBlock:^(id responseObject) {
                NSLog(@"&&&&&&------------%@",responseObject);
                signup_profile_view.acitvationCode=responseObject[@"data"][@"activationKey"];
                
                [self.navigationController pushViewController:signup_profile_view animated:YES];
            }];
        }
        else{
            [UIView showToastWindowWithMessage:@"验证码不能为空"];
        }
    }
    else{
        [UIView showToastWindowWithMessage:@"密码不能为空"];
    }
}

#pragma mark 网络请求回调
-(void)OnStartWithRequestTag:(int)tag{
    [UIView showActivityViewWithView:self.view msg:nil];
}

-(void)OnSuccess:(id)responseObject requestTag:(int)tag{
    NSLog(@"~~~~~~~~~~~~~~~~~~~~~%@",responseObject);
    
}

-(void)OnFail:(NSString *)errorMsg errorCode:(NSInteger)errorCode requestTag:(int)tag{
    
    NSLog(@"%@---------%ld",errorMsg,errorCode);
    
    [UIView showToastWithMessage:[errorMsg isNotEmpty]?errorMsg:@"网络连接异常" andViewController:self];
}

-(void)OnFinishWithRequestTag:(int)tag{
    [UIView hideActivityView];
}

@end
